<?php
namespace app\member\controller\sys_admin;
use app\member\model\DistributionStoreModel;
use think\Db;

use app\AdminController;
use app\member\model\WithdrawModel;
use app\member\model\WithdrawAccountModel;
use app\member\model\UsersModel;
use app\member\model\AccountLogModel;
//*------------------------------------------------------ */
//-- 门店审核
/*------------------------------------------------------ */
class DistributionStore extends AdminController
{
	 //*------------------------------------------------------ */
	//-- 初始化
	/*------------------------------------------------------ */
   public function initialize(){	
   		parent::initialize();
		$this->Model = new DistributionStoreModel();
    }
	/*------------------------------------------------------ */
	//-- 主页
	/*------------------------------------------------------ */
    public function index(){
        $data = Db::name('distribution_store')->select();

        foreach($data as $k1 =>$v1){
            $merger_name= Db::name('main_region')->where('id',$v1['district_id'])->value('merger_name');

                $data[$k1]['merger_name'] = $merger_name;

        }
		$this->assign("data", $data);
		return $this->fetch();
	}
   /*------------------------------------------------------ */
    //-- 获取列表
	//-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false) {

        $data = Db::name('distribution_store')->select();

        $this->assign("data", $data);
        return $this->success('','',$data);
    }

	/*------------------------------------------------------ */
	//-- 信息页调用
	//-- $data array 自动读取对应的数据
	/*------------------------------------------------------ */
	public function asInfo($data){
	    $data['merger_name'] = Db::name('main_region')->where(['id'=>$data['district_id']])->value('merger_name');
        $province = Db::name('main_region')->where('level_type',1)->field('id,name')->select();
        $city = Db::name('main_region')->where('level_type',2)->field('id,name')->select();
        $district = Db::name('main_region')->where('level_type',3)->field('id,name')->select();
        $this->assign('province', $province);
        $this->assign('city', $city);
        $this->assign('district', $district);
//        dump($data);exit;
		return $data;
	}
	
	/*------------------------------------------------------ */
	//-- 修改前处理
	/*------------------------------------------------------ */
    public function beforeEdit($data){
//        dump($data);
//        dump(input(''));exit;
        if($data['status'] == ''){
            return $this->error('请选择审核状态');
        }
        if(!$data['store_id']){
            return $this->error('网络错误，请稍后再试');
        }
		$data['store_id'] = $data['store_id'];
		$data['status'] = $data['status'];

        if($data['status'] == 1){
            $district_id = Db::name('distribution_store')->where(['store_id'=>$data['store_id'],'status'=>1])->where('user_id','<>',$data['user_id'])->value('district_id');
            if($district_id) return $this->error('该店铺已存在.');
        }
        $changedata['store_name'] = $data['store_name'];
        $changedata['store_phone'] = $data['store_phone'];
        $changedata['identity_card'] = $data['identity_card'];
        $changedata['store_id'] = $data['store_id'];
        $changedata['province_id'] = $data['province_id'];
        $changedata['city_id'] = $data['city_id'];
        $changedata['district_id'] = $data['district_id'];
        $changedata['lng'] = $data['lng'];
        $changedata['lat'] = $data['lat'];
        $changedata['store_address'] = $data['store_address'];
        $changedata['introduction'] = $data['introduction'];
        $changedata['store_img'] = $data['store_img'];
        $changedata['identity_img_one'] = $data['identity_img_one'];
        $changedata['identity_img_tow'] = $data['identity_img_tow'];
        $changedata['business_license'] = $data['business_license'];
        $changedata['store_logo_img'] = $data['store_logo_img'];
        $changedata['store_id'] = $data['store_id'];
        $changedata['status'] = $data['status'];
        $changedata['update_time'] = time();
//        dump($changedata);exit;
        $res = Db::name('distribution_store')->where('store_id',$data['store_id'])->update($changedata);
        if ($res !== false) {
            return $this->success('操作成功.',url('index'));
        }else{
            Db::rollback();// 回滚事务
            return $this->error('未知错误，审核失败.');
        }
        Db::commit();// 提交事务
		Db::startTrans();//启动事务
		return $data;		
	}
	/*------------------------------------------------------ */
	//-- 修改后处理
	/*------------------------------------------------------ */
    public function afterEdit($data){
//        dump($data);
//        dump(input(''));exit;


	}
}
